clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table3.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

timer on 1

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

capture noisily mkdir "`root'\Results"
capture noisily mkdir "`root'\Results\Final_code_RCFS"
cd "`root'\Results\Final_code_RCFS"

use `finalfile_yearly', clear
drop if neverpayer == 1

generate avg_logassets_sqY = avg_logassetsY*avg_logassetsY
regress avg_logassets_sqY avg_logassetsY
predict sizesq_orth, residuals
label variable avg_logassetsY "Size (log assets)"
label variable sizesq_orth "Size (log assets) squared, orthogonalized"

rename avg_divdivbookvalueY divbookY
rename divpayoutratioYALT divpayoutY
rename avg_divpayoutratioY divpayoutY_SNL
rename divpershare_year divpershY

winsor tpopshY1, generate(tpopshY1W) p(0.01)

generate avg_tassetsW_BN = avg_totassetsWY/(10^9)

generate avg_tassetsW_BN_sq = avg_tassetsW_BN * avg_tassetsW_BN

xtset instkey year

generate HMDA_to_total3 = (loanvalue + L.loanvalue + L2.loanvalue)/((gross_lns + L.gross_lns + L2.gross_lns)/3)
generate HMDA_to_total2 = (loanvalue + L.loanvalue)/((gross_lns + L.gross_lns)/2)

generate lenientV3 = (loanvalue + L.loanvalue + L2.loanvalue) / (appvalue + L.appvalue + L2.appvalue)
generate lenientC3 = (loancount + L.loancount + L2.loancount) / (appcount + L.appcount + L2.appcount)

generate lenientV2 = (loanvalue + L.loanvalue) / (appvalue + L.appvalue)
generate lenientC2 = (loancount + L.loancount) / (appcount + L.appcount)

foreach var of varlist HMDA_to_total3 lenientV3 lenientC3 {
	generate chg_`var' = D3.`var'
	}

foreach var of varlist HMDA_to_total2 lenientV2 lenientC2 {
	generate chg_`var' = D2.`var'
	}

// Make a new variable that captures analyst coverage, including zeroes

generate numest_zeroes = numest
replace numest_zeroes = 0 if numest == .

// Analyst dummy

generate analyst_dummy = 0
replace analyst_dummy = 1 if numest != .

// Generate lag variables to line these up with the 2004-2006 period (since we sort on year 2008 in the quantiles)

generate L2HMDA_to_total3 = L2.HMDA_to_total3 
generate L2lenientV3 = L2.lenientV3

label variable avg_tassetsW_BN "Total assets (bn $)"
label variable avg_tassetsW_BN_sq "Total assets squared (bn $)"
label variable avg_mtbWY "Market to book ratio"
label variable avg_roaaWY "ROA"
label variable earningsvol "Earnings volatility"
label variable lev_fps "Leverage"
label variable avg_rwcapitalratioWY "Risk-weighted capital ratio"

****** Table 3

foreach y of varlist divpshY1W avg_divyieldY tpopshY1W  avg_tpoyieldY  {
	
local rhs L.avg_logassetsY L.sizesq_orth  ///
L.avg_mtbWY L.avg_roaaWY L.lev_fps L.reteY growth2WY L.avg_tieroneratioWY

		if "`y'" == "avg_divyieldY" {	// Redefine rhs to take the MTB out for dividend yield
			local rhs : subinstr local rhs "L.avg_mtbWY" ""
			}

		if "`y'" == "avg_tpoyieldY" {	// Redefine rhs to take the MTB out for total payout yield
			local rhs : subinstr local rhs "L.avg_mtbWY" ""
			}
		
		quietly xtreg `y' `rhs' if year < 2007, fe vce(cluster instkey) 				
	
		* 1) Get the residuals
		predict Ef_`y', e
		generate E_95to06 = Ef_`y' 

		predict HATf_`y'
		predict STDPf_`y', stdp
		predict UEf_`y', ue
		predict Uf_`y', u
		
		* Need to extend the FE to all years
		by instkey: egen aaa = max(Uf_`y')
		generate bbb = Uf_`y'
		replace Uf_`y' = aaa if bbb == .
		drop aaa bbb

		generate ccc = Ef_`y'
		replace Ef_`y' = UEf_`y' - Uf_`y'
		drop ccc
		generate predictedf_`y' = HATf_`y' + Uf_`y'	// Predicted values (including the FE)
		gen a = HATf_`y' + Uf_`y' + Ef_`y'		// Check that the sums work 
		corr a `y'
		drop a

		generate E_07to09_out = Ef_`y'							// these are the out-of-sample residuals
		replace E_07to09_out = . if year < 2007 | year > 2009		// make sure they are empty out of sample

************************************ During the crisis **************************		
		
		quietly xtreg `y' `rhs' /*L.divpshY1W*/ if year >= 2007 & year < 2009, fe vce(cluster instkey) 				
		capture noisily drop HAT*_* STDP*_* UE*_* U*_* Ef_`y'
		
		* 1) Get the residuals
		
		predict Ef_`y', e
		rename Ef_`y' E_07to09
		quietly summarize E_07to09
		generate stE_07to09 = (E_07to09 - r(mean)) / r(sd)
		
		pwcorr E_07to09_out E_07to09, star(0.05)			// check if the in-sample and out-of-sample residuals are correlated


************************************ During and after the crisis **************************
		
		quietly xtreg `y' `rhs' if year >= 2007, fe vce(cluster instkey) 				
		capture noisily drop STDP*_ UE*_ U*_ E*_
		
		* 1) Get the residuals
		predict Ef_`y', e
		
		rename Ef_`y' E_07to12
		quietly summarize E_07to12
		generate stE_07to12 = (E_07to12 - r(mean)) / r(sd)
	
	forvalues t = 2007/2008 {

		sum E_95to06, detail
		local avg95to06 = r(mean)
		sum E_07to09 if year == `t', detail
		local avgin`t' = r(mean)
		sum E_07to09_out if year == `t', detail
		local avgout`t' = r(mean)
		
		di "Average actual `y' in the year `t'"
		summarize `y' 
		local sdY = r(sd)
		di "Average predicted `y' in the year `t'"
		summarize predictedf_`y' if year == `t' , detail
		local avgYhat = r(mean)
		regress E_07to09_out if year == `t'	
		local averageYhat_scaled = _b[_cons]/`sdY'
				
		generate diff_E_inVSout = E_07to09 - E_07to09_out
		regress E_07to09_out if year == `t'									// Test whether the residual is different from 0 
		outreg2 using Table3, ///											// It's written as a regression for convenience
		excel stats(coef tstat) par(tstat) dec(2) ctitle(`y' in `t' out of sample)			/// Equivalent to ttest E == 0
		adds(% of one standard deviation, `averageYhat_scaled')
		drop diff_E_inVSout

		}
		
		drop E_95to06 E_07to09 E_07to12 E_07to09_out stE_07to09 stE_07to12
	
}	
	
